﻿# -*- coding: utf-8 -*-
'''
**********************************************************************
****************  smpy: Scientific Modelling in Python  **************
**********************************************************************
Copyright (c) 2013, Sergei Mingaleev (http://mingaleev.nanoscience.by)
All rights reserved.

Redistribution and use in source and binary forms, with or without 
modification, are permitted provided that the following conditions 
are met:

 * Redistributions of source code must retain the above copyright 
   notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright 
   notice, this list of conditions and the following disclaimer 
   in the documentation and/or other materials provided with the 
   distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
POSSIBILITY OF SUCH DAMAGE.

smpy:
   Library that facilitates simulation of physical devices and 
   systems, starting with support of physical units, materials, 
   and layouts.

smpy is licensed under the terms of the BSD 2-Clause License.
In its turn, it reuses the following BSD compatible code:
 * Pint: a Python units library
      Author: Hernan E. Grecco
      Home Page: https://github.com/hgrecco/pint
	             https://pypi.python.org/pypi/Pint
      License: BSD
 * minivec: Pure python vector and matrix
      Author: Peter Shinners
      Home Page: http://code.google.com/p/minivec/
      License: MIT
'''

__author__="Sergei Mingaleev (http://mingaleev.nanoscience.by)"
__license__ = "BSD 2-Clause License"
__version__ = "0.1"

if __doc__:
	__doc__ += '''
Subpackages:
------------

	constants:
		Physical constants like speed of light c, magentic and electric 
		constants mu0 and epsilon0, etc.

Utility tools:
--------------

'''

#-------------------------------------------------------------------
# Handlers for errors, warnings, and other messages to users:

# allowed levels of verbosity:
verbosity_level = 1
debug_level = 0

def error(msg, ExceptionType=Exception):
	'''
	Deliver an error message to user.
	You can adapt this function for your own needs (say, add logging of all the messages).
	'''
	raise ExceptionType(msg)

def message(msg, level=1):
	'''
	Deliver an informational message with given priority 'level' to user.
	'''
	if level<=verbosity_level:
		print(msg)

def debug(msg, level=1):
	'''
	Deliver a debug message with given verbosity 'level' to user.
	'''
	if level<=debug_level:
		message("DEBUG: {0}".format(msg))

#-------------------------------------------------------------------
# Let's add our own exceptions:

class IllegalAction(Exception):
	pass

class WrongFileFormat(Exception):
	pass

#-------------------------------------------------------------------
class object(object):
	'''
	Our own version of the base object class - 
	with added support for handling errors and debug messages.
	'''
	def __init__(self):
		global debug_level
		self.debug_level=debug_level
	def error(self, msg, ExceptionType=Exception):
		raise ExceptionType("{0}: {1}".format(self.__class__, msg))
	def ensure(self, condition, msg):
		assert condition, '{0}: {1}'.format(self.__class__, msg)
	def debug(self, msg, dlevel=1):
		global message
		if dlevel<=self.debug_level:
			message("DEBUG {0}: {1}".format(self.__class__, msg))
	def warning(self, msg):
		global message
		message("WARNING {0}: {1}".format(self.__class__, msg))

#-------------------------------------------------------------------
# Added for compatibilitry with Python 3, which has no "execfile" command:
def execfile(filename, global_vars={}, local_vars={}):
	with open(filename) as f:
		code = compile(f.read(), filename, 'exec')
		exec(code, global_vars, local_vars)

#-------------------------------------------------------------------
